//
//  NSAttributedString-Extension.swift
//  Chihiro
//
//  Created by point on 2016/11/17.
//  Copyright © 2016年 chihiro. All rights reserved.
//

import UIKit


extension NSAttributedString {
    
    //文字行高
    class func lineSpacing(_ content: String ,color: UIColor, _ space: Int) -> NSAttributedString {
        let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: content)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = CGFloat(space)
        paragraphStyle.alignment = .left
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, content.count))
        attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: NSMakeRange(0, content.count))
        return attributedString
    }
    
    //计算文字大小
    class func getTextRectSize(text: NSString,font: UIFont,size: CGSize) -> CGRect {
        let attributes = [NSAttributedString.Key.font: font]
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        let rect:CGRect = text.boundingRect(with: size, options: option, attributes: attributes, context: nil)
        return rect;
    }
    
    
    
    /// 设置富文本 线 大小 行间距 颜色
    ///
    /// - Parameters:
    ///   - text: 所有文字
    ///   - index: index前面包含index文字长度
    ///   - aC: index前面包含index文字颜色
    ///   - bC: index后不包含index的文字颜色
    ///   - aF: index前面包含index文字长度大小
    ///   - bF: index后不包含index文字长度大小
    ///   - line: 中划线
    /// - Returns: 富文本
    class func getAttributedString( text:String ,index:Int, aC:UIColor,bC:UIColor,aF:CGFloat ,bF:CGFloat , line:Bool ,lineSpacing:CGFloat ) -> NSAttributedString {
        if text.count == 0 {
            return  NSMutableAttributedString.init(string: "")
        }
        
        let attributeText = NSMutableAttributedString.init(string: text)
        let count = text.count
        attributeText.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: aF), range: NSMakeRange(0, index))
        attributeText.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: bF), range: NSMakeRange(index, count-index))
        attributeText.addAttribute(NSAttributedString.Key.foregroundColor, value: aC, range: NSMakeRange(0, index))
        attributeText.addAttribute(NSAttributedString.Key.foregroundColor, value: bC, range: NSMakeRange(index, count-index))
        
        //行间距
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        attributeText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, text.count))
        
        //下划线
        if line {
            attributeText.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: NSRange(location: index, length: count - index))
            attributeText.addAttribute(NSAttributedString.Key.baselineOffset, value: NSNumber(value: 1), range: NSMakeRange(index, count - index))
        }
        return attributeText
    }
    
    
    
    
    class func getTextRectSize(content: String,index:Int,aF:CGFloat ,bF:CGFloat ,lineSpacing:CGFloat ,size: CGSize ) -> CGRect {
        
        let attributes = NSMutableAttributedString(string: content)
        let count = content.count
        
        //行高
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 10
        attributes.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, content.count))
        
        //前后文字的大小
        attributes.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: aF), range: NSMakeRange(0, index))
        attributes.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: bF), range: NSMakeRange(index, count-index))
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        return attributes.boundingRect(with: size, options: option, context: nil)
    }
}
